using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;

using HunterPluginSDK;
using HunterPluginSDK.Interface;


namespace MyHunter.Details
{
    
    /// <summary>
    /// Control to display given Collecntion content
    /// </summary>
    public partial class Collection : DockContent, 
        IProgressHandler,
        MyHunter.Interfaces.ICollection
    {
        #region MEMBERS
        public DataSchema.CollectionSchema Coll;
        private Engine.Factory.CollectionFactory MyFactory = null;
        private IProgressHandler ProgressHandler;
        #endregion

        #region CTOR
        public Collection(IProgressHandler h, string path)
        {
            InitializeComponent();
            ProgressHandler = h;
            Init();

            Coll.ReadXml(path);
            Coll.AcceptChanges();

            MyFactory = Engine.Factory.CollectionFactory.OpenCollection(Coll, this);

            this.WriteToOutput("Collection " + this.CollectionName + " was opened", LogPriority.ACTIONHISTORY);

            SetDefault();
        }

        public Collection(IProgressHandler h, New.NewCollection nc)
        {
            InitializeComponent();
            ProgressHandler = h;
            Init();

            MyFactory = Engine.Factory.CollectionFactory.CreateNewCollection
                 (Coll, this, nc.CollectionName, nc.Description, nc.SiteName, nc.CsvFolder, nc.CollectionPath);

            Coll.AcceptChanges();
            
            this.WriteToOutput("Collection " + this.CollectionName + " was created", LogPriority.ACTIONHISTORY);

            SetDefault();
        }

        #endregion

        #region PRIVATE
        private void Init()
        {
            Coll = new DataSchema.CollectionSchema();
            
            this.comboBoxSite.DataSource = DataSchema.Settings.SettingsMgr.Settings.Sites;
            this.comboBoxSite.DisplayMember = "Name";
            this.comboBoxSite.ValueMember = "Name";

            BuildGrid();
        }
        
        private void SetDefault()
        {
            UpdateTotalInfo();
            this.folderBrowserDialog.SelectedPath = this.Coll.GetDefaultIncomingFolder();
            this.labelLH.Text = "Last Hunted: " + this.Coll.GetLastHuntDT();
        }

        private void BuildGrid()
        {
            colEntitySize.DefaultCellStyle.FormatProvider = new Engine.IO.FileSizeFormatter();
            colEntitySize.DefaultCellStyle.Format = "S";
            System.Windows.Forms.DataGridViewCheckBoxColumn Pics;
            System.Windows.Forms.DataGridViewTextBoxColumn NameCol;
            System.Windows.Forms.DataGridViewTextBoxColumn Description;
            System.Windows.Forms.DataGridViewTextBoxColumn Rank;
            System.Windows.Forms.DataGridViewTextBoxColumn Total;
            System.Windows.Forms.DataGridViewTextBoxColumn Hunted;
            System.Windows.Forms.DataGridViewCheckBoxColumn ST;
            System.Windows.Forms.DataGridViewCheckBoxColumn SS;
            System.Windows.Forms.DataGridViewCheckBoxColumn HS;
            System.Windows.Forms.DataGridViewCheckBoxColumn SL;
            System.Windows.Forms.DataGridViewCheckBoxColumn HL;
            System.Windows.Forms.DataGridViewCheckBoxColumn SB;
            System.Windows.Forms.DataGridViewCheckBoxColumn HB;
            System.Windows.Forms.DataGridViewCheckBoxColumn AT;
            System.Windows.Forms.DataGridViewCheckBoxColumn AS;
            System.Windows.Forms.DataGridViewCheckBoxColumn DP;
            System.Windows.Forms.DataGridViewCheckBoxColumn FM;
            System.Windows.Forms.DataGridViewCheckBoxColumn PS;


            NameCol = new System.Windows.Forms.DataGridViewTextBoxColumn();
            Description = new System.Windows.Forms.DataGridViewTextBoxColumn();
            Rank = new System.Windows.Forms.DataGridViewTextBoxColumn();
            Total = new System.Windows.Forms.DataGridViewTextBoxColumn();
            Hunted = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ST = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            SS = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            HS = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            SL = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            HL = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            SB = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            HB = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            AT = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            AS = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            DP = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            FM = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            PS = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            Pics = new System.Windows.Forms.DataGridViewCheckBoxColumn();


            // 
            // NameCol
            // 
            NameCol.DataPropertyName = "Name";
            NameCol.HeaderText = "Name";
            NameCol.Name = "NameCol";
            // 
            // Description
            // 
            Description.DataPropertyName = "Description";
            Description.HeaderText = "Description";
            Description.Name = "Description";
            // 
            // Rank
            // 
            Rank.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            Rank.DataPropertyName = "Rank";
            Rank.HeaderText = "Rate";
            Rank.Name = "Rank";
            Rank.Width = 55;
            // 
            // Total
            // 
            Total.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            Total.DataPropertyName = "Total";
            Total.HeaderText = "Total";
            Total.Name = "Total";
            Total.Width = 56;
            // 
            // Hunted
            // 
            Hunted.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            Hunted.DataPropertyName = "Hunted";
            Hunted.HeaderText = "Hunted";
            Hunted.Name = "Hunted";
            Hunted.Width = 67;
            // 
            // ST
            // 
            ST.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            ST.DataPropertyName = "ST";
            ST.FillWeight = 50F;
            ST.HeaderText = "ST";
            ST.Name = "ST";
            ST.Width = 27;
            // 
            // SS
            // 
            SS.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            SS.DataPropertyName = "SS";
            SS.FillWeight = 50F;
            SS.HeaderText = "SS";
            SS.Name = "SS";
            SS.Width = 27;
            // 
            // HS
            // 
            HS.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            HS.DataPropertyName = "HS";
            HS.FillWeight = 50F;
            HS.HeaderText = "HS";
            HS.Name = "HS";
            HS.Width = 28;
            // 
            // SL
            // 
            SL.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            SL.DataPropertyName = "SL";
            SL.FillWeight = 50F;
            SL.HeaderText = "SL";
            SL.Name = "SL";
            SL.Width = 26;
            // 
            // HL
            // 
            HL.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            HL.DataPropertyName = "HL";
            HL.HeaderText = "HL";
            HL.Name = "HL";
            HL.Width = 27;
            // 
            // SB
            // 
            SB.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            SB.DataPropertyName = "SB";
            SB.HeaderText = "SB";
            SB.Name = "SB";
            SB.Width = 27;
            // 
            // HB
            // 
            HB.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            HB.DataPropertyName = "HB";
            HB.HeaderText = "HB";
            HB.Name = "HB";
            HB.Width = 28;
            // 
            // AT
            // 
            AT.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            AT.DataPropertyName = "AT";
            AT.HeaderText = "AT";
            AT.Name = "AT";
            AT.Width = 27;
            // 
            // AS
            // 
            AS.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            AS.DataPropertyName = "AS";
            AS.HeaderText = "AS";
            AS.Name = "AS";
            AS.Width = 27;
            // 
            // DP
            // 
            DP.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            DP.DataPropertyName = "DP";
            DP.HeaderText = "DP";
            DP.Name = "DP";
            DP.Width = 28;

            // 
            // FM
            // 
            FM.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            FM.DataPropertyName = "FM";
            FM.HeaderText = "FM";
            FM.Name = "FM";
            FM.Width = 27;
            // 
            // PS
            // 
            PS.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            PS.DataPropertyName = "PS";
            PS.HeaderText = "PS";
            PS.Name = "PS";
            PS.Width = 28;

            // 
            // Pics
            // 
            Pics.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
            Pics.DataPropertyName = "Pics";
            Pics.HeaderText = "Pics";
            Pics.Name = "Pics";
            Pics.Width = 28;



            this.dataGridViewShot.Columns.Clear();
            this.dataGridViewShot.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            Pics,
            NameCol,
            Description,
            Rank,
            Total,
            Hunted,
            ST,
            SS,
            HS,
            SL,
            HL,
            SB,
            HB,
            AT,
            AS,
            DP,
            FM,
            PS});

        }
 
        private void EndEdit()
        {
            Coll.Collection.Rows[0].EndEdit();

            this.dataGridViewEntity.EndEdit();
            this.dataGridViewIncoming.EndEdit();
            this.dataGridViewShot.EndEdit();
            this.dataGridViewItem.EndEdit();
        }

        #endregion

        #region IProgressHandler

        public void LogException(Exception ex)
        {
            this.LogException(ex);
        }

        public int TotalProgress
        {
            set
            {
                if(value == -1)
                {//it's stoping hunting
                    HuntingIsFinished();
                    value = 0;
                }
                this.ProgressHandler.TotalProgress = value;
            }
        }

        public void ProgressChanged()
        {
            this.ProgressHandler.ProgressChanged();
        }

        public string ProgressDesription
        {
            set
            {
                 this.ProgressHandler.ProgressDesription = value;
            }
        }

        public void WriteToOutput(string txt, LogPriority prior)
        {
            this.ProgressHandler.WriteToOutput(txt, prior);
        }


        public string WatcherDescription
        {
            set
            {
                this.ProgressHandler.WatcherDescription = value;
            }
        }

        public void WatcherChanged()
        {
            this.ProgressHandler.WatcherChanged();
        }

        #endregion

        #region THREAD UPDATE
        public  void HuntingIsFinished()
        {
            this.Enabled = true;

            this.dataGridViewEntity.DataSource = collectionEntityDataTableBindingSource;

            this.dataGridViewShot.DataSource = shotDataTableBindingSource;

            this.dataGridViewItem.DataSource = itemDataTableBindingSource;

            this.Coll.UpdateLastHuntDT();
            UpdateTotalInfo();

            MyHunter.Log.LogForm l = new MyHunter.Log.LogForm(this.Coll.LastHuntLog,
                this.Coll.FinishedShots, this.Coll.FinishedEntities);

            l.Show();
        }
        #endregion

        #region PUBLIC
        public void Stop()
        {
            this.MyFactory.ContinueHunting = false;
            this.MyFactory.ContinueBackup = false;
        }

        public void ShowCollection()
        {
            this.Text = this.CollectionName;

            collectionEntityDataTableBindingSource.DataSource = Coll;
            collectionEntityDataTableBindingSource.DataMember = "CollectionEntity";
            
            this.shotDataTableBindingSource.DataSource = collectionEntityDataTableBindingSource;
            this.shotDataTableBindingSource.DataMember = "FK_CollectionEntity_Shot";
            
            this.itemDataTableBindingSource.DataSource = shotDataTableBindingSource;
            this.itemDataTableBindingSource.DataMember = "FK_Shot_Item";
            
            this.incomingFoldersDataTableBindingSource.DataSource = Coll.IncomingFolders;
            
            this.collectionDataTableBindingSource.DataSource = Coll.Collection;

            Coll.Collection.Rows[0].BeginEdit();

            this.dataGridViewEntity.Sort(nameDataGridViewTextBoxColumn, ListSortDirection.Ascending);
        }


        public void ManageEntity()
        {
            bool error = false;
            this.dataGridViewEntity.SuspendLayout();
            New.NewEntity ne = new New.NewEntity(Coll, Coll.Collection.Rows[0]["CSVFolderPath"].ToString());
            if(ne.ShowDialog() == DialogResult.OK)
            {
                this.WriteToOutput("Start Managing " + this.CollectionName + " collection", LogPriority.INFO);
                foreach (string csv in ne.CSVList)
                {
                    try
                    {
                        this.WriteToOutput("Start Managing " + csv + " entity", LogPriority.INFO);
                        MyFactory.CreateNewCollectionEntity(csv);
                        this.WriteToOutput("Stop Managing " + csv + " entity", LogPriority.INFO);
                    }
                    catch(DataSchema.DuplicatedObjectException e)
                    {
                        this.WriteToOutput(e.ToString(), LogPriority.ERROR);
                        error = true;
                    }
                    catch(Exception ex)
                    {
                        string i = "Can't add " + ex.ToString() + " entity";
                        this.WriteToOutput(i, LogPriority.CRITICAL);
                        error = true;
                    }
                }
                this.WriteToOutput("Stop managing " + this.CollectionName + " collection", LogPriority.INFO);
                foreach (string csv in ne.CSVListRemove)
                {
                    MyFactory.RemoveEntity(csv, Coll);
                    this.WriteToOutput("Entity " + csv + " was removed from " + this.CollectionName, LogPriority.INFO);
                }
                if(error)
                    HunterForm.ShowMsg("There were some error. See output for more details.", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.dataGridViewEntity.ResumeLayout();
        }

        public void UpdateCSV()
        {
            dataGridViewEntity.EndEdit();
            try
            {
                this.WriteToOutput("Begin updating " + this.CollectionName + " collection", LogPriority.INFO);
                MyFactory.UpdateCollectionEntity();
                this.WriteToOutput("End updating " + this.CollectionName + " collection", LogPriority.INFO);
            }
            catch(Exception ex)
            {
                string i = "Can't update entity because of " + ex.ToString();
                HunterForm.ShowMsg(i, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.WriteToOutput(i, LogPriority.CRITICAL);
            }
        }

        public void CreateNeededReport()
        {
            MyFactory.CreateNeededReport();
        }

        public void OpenNeededReport()
        {
            string colpath = "explorer";
            Engine.IO.FileTools.OpenFile(colpath, this.CollectionPath + "Needed\\", true);
        }

        public void CreateLastHuntReport()
        {
            Log.LogForm lg = new MyHunter.Log.LogForm(this.Coll.LastHuntLog, this.Coll.FinishedShots, this.Coll.FinishedEntities);
            lg.ShowDialog();
 
        }

        public void Hunt(Engine.CSV.ProcessType pr)
        {
            Coll.LastHuntLog.Rows.Clear();
            
            EndEdit();
            
            this.dataGridViewEntity.DataSource = null;
            this.dataGridViewShot.DataSource = null;
            this.dataGridViewItem.DataSource = null;

            this.Enabled = false;

            MyFactory.ContinueHunting = true;
            MyFactory.Hunt(pr);
        }

        public bool Goto2Shot(DataSchema.CollectionSchema.ShotRow sr)
        {
            int i = 0, e = 0;

            foreach (DataRowView rve in collectionEntityDataTableBindingSource.List)
            {
                if(rve.Row["Name"].ToString() == sr.EntityName)
                {
                    collectionEntityDataTableBindingSource.Position = e;
                    foreach (DataRowView rv in shotDataTableBindingSource.List)
                    {
                        if (rv.Row == sr)
                        {
                            shotDataTableBindingSource.Position = i;
                            return true;
                        }
                        ++i;
                    }
                }
                e++;
            }

            
            return false;

            i = 0;

            foreach (DataGridViewRow re in this.dataGridViewShot.Rows)
            {
                if (re.Cells[0].Value.ToString() == sr.Name)
                    break;
                ++i;
            }
            shotDataTableBindingSource.Position = i;

        }

        public void Burn()
        {
            MyFactory.Burn();
        }

        public void BackUp()
        {
            EndEdit();

            Tools.BackUp b = new Tools.BackUp(this.Coll.Backup);
            if(b.ShowDialog() == DialogResult.OK)
            {
                if(MyFactory.ContinueBackup == true)
                {
                    HunterForm.ShowMsg("Backup in progress. Wait a moment", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }
                MyFactory.ContinueBackup = true;
                this.MyFactory.BackUp();
            }
        }

        #endregion

        #region EVENTS

        private void buttonDetails_Click(object sender, EventArgs e)
        {

        }


        private void OnItemClick(object sender, DataGridViewCellEventArgs e)
        {
            DataSchema.CollectionSchema.ItemRow ir = 
                (DataSchema.CollectionSchema.ItemRow)
                ((DataRowView)this.dataGridViewItem.Rows[e.RowIndex].DataBoundItem).Row;

            string imgpath = Coll.GetItemPath( ir );
            if(Engine.IO.FileTools.OpenFile(imgpath, "", false) == false)
            {
                HunterForm.ShowMsg("Can't open this file", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void buttonSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataSchema.CollectionSchema.CollectionEntityRow er in Coll.CollectionEntity.Rows)
            {
                er.Selected = true;
            }
        }

        private void buttonSelectUncomplete_Click(object sender, EventArgs e)
        {
            foreach (DataSchema.CollectionSchema.CollectionEntityRow er in Coll.CollectionEntity.Rows)
            {
                if(er.Completed == false)
                    er.Selected = true;
            }
        }

        private void buttonDeselect_Click(object sender, EventArgs e)
        {
            foreach (DataSchema.CollectionSchema.CollectionEntityRow er in Coll.CollectionEntity.Rows)
            {
                er.Selected = false;
            }

        }

        private void OnFileSizeFormat(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridViewColumn col = this.dataGridViewEntity.Columns[e.ColumnIndex];
            if (col.DefaultCellStyle.FormatProvider is ICustomFormatter)
            {
                Object originalData = e.Value;
                e.Value = ((ICustomFormatter)(col.DefaultCellStyle.FormatProvider)).Format("", e.Value, col.DefaultCellStyle.FormatProvider);
                //e.Value = String.Format(col.DefaultCellStyle.FormatProvider, col.DefaultCellStyle.Format, originalData);
            }
        }

        private void btnSelectIncoming_Click(object sender, EventArgs e)
        {
            foreach (DataSchema.CollectionSchema.IncomingFoldersRow ir in Coll.IncomingFolders.Rows)
            {
                ir.Selected = true;
            }
        }

        private void btnDeselectIncoming_Click(object sender, EventArgs e)
        {
            foreach (DataSchema.CollectionSchema.IncomingFoldersRow ir in Coll.IncomingFolders.Rows)
            {
                ir.Selected = false;
            }
        }

        private void buttonAddNewIncoming_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Coll.AddNewIncomingFolder(folderBrowserDialog.SelectedPath);
                }
                catch(DataSchema.DuplicatedObjectException ex)
                {
                    HunterForm.ShowMsg(ex.Reason, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch(Exception ex)
                {
                    this.WriteToOutput(ex.ToString(), LogPriority.CRITICAL);
                }
            }
        }

        private void OnCollectionLoad(object sender, EventArgs e)
        {
            ShowCollection();
        }

        private void OnPaintRow(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            DataGridViewRow theRow1 = ((DataGridView)sender).Rows[e.RowIndex];
            //throw new System.Exception();
            DataRowView theRow = (DataRowView)theRow1.DataBoundItem;
            if(theRow != null)
            {
               if(theRow.Row.Table.Columns.Contains("Burned") && ((bool)(theRow["Burned"])) == true)
               {
                   theRow1.DefaultCellStyle.BackColor = Color.DarkGreen;
                   return;
               }

                if(theRow["Hunted"].ToString() == "0")
                {
                    theRow1.DefaultCellStyle.BackColor = Color.White;
                }
                else if(theRow["Hunted"].ToString() == theRow["Total"].ToString()
                    || theRow.Row.Table.Columns.Contains("Completed") && ((bool)(theRow["Completed"])) == true)
                {
                    theRow1.DefaultCellStyle.BackColor = Color.LightGreen;
                }
                else
                    theRow1.DefaultCellStyle.BackColor = Color.LightSkyBlue;


            }
        }

        private void UpdateTotalInfo()
        {
            UpdateUI ui = delegate()
            {
                this.labelTotal.Text = "Total: " + Coll.TotalHunted.ToString();

                this.labelTotal.Text +=  "/" + Engine.IO.FileTools.GetSize(Coll.TotalSize);

                this.labelLH.Text = "Last Hunted: " + this.Coll.GetLastHuntDT();
            };

            if (InvokeRequired)
                this.Invoke(ui);
            else
                ui();
        }

        private void OnClosing(object sender, FormClosingEventArgs e)
        {
            if(this.Enabled == false)//it is running
            {
                MessageBox.Show("There is some job in progress for " + this.CollectionName+ "?", "Shark's Hunter", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                e.Cancel = true;
            }

            if (Coll.HasChanges())
            {
                DialogResult dr = MessageBox.Show("Would you like to save changes in " + this.CollectionName+ "?", "Shark's Hunter", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk);
                if (dr == DialogResult.Yes)
                {
                    this.SaveCollection();
                    this.WriteToOutput("Collection " + this.CollectionName + " was closed." , LogPriority.ACTIONHISTORY);
                }
                else if (dr == DialogResult.Cancel)
                    e.Cancel = true;
            }
        }
        private void buttonSelectHighlighted_Click(object sender, EventArgs e)
        {
            foreach(DataGridViewRow dgvr in this.dataGridViewEntity.SelectedRows)
            {
                dgvr.Cells[0].Value = true;
            }
        }


        #endregion

        #region ICollection
        
        public string CollectionName
        {
            get
            {
                return Coll.Collection.Rows[0]["Name"].ToString();
            }
        }
        public string CollectionSite
        {
            get
            {
                return Coll.Collection.Rows[0]["Site"].ToString();
            }
        }
        public string CollectionPath
        {
            get
            {
                return Coll.Collection.Rows[0]["Path"].ToString();
            }
        }

        public bool Modified
        {
            get
            {
                return this.Coll.HasChanges();
            }
        }
        public void SaveCollection()
        {
            EndEdit();

            string colpath = Coll.Collection.Rows[0]["Path"].ToString();
            
            if(Coll.HasChanges())
                Coll.WriteXml(colpath  + CollectionName + DataSchema.DataMgr.EXTDS);

            DataSchema.DataMgr.Serialize(Coll, colpath + CollectionName + DataSchema.DataMgr.EXTOBJ);

            Coll.AcceptChanges();
            this.WriteToOutput("Collection " + this.CollectionName + " was saved." , LogPriority.ACTIONHISTORY);
        }

        public DataSchema.CollectionSchema.ShotRow[] FindShot(string sql, List<string> fi, bool link)
        {
            DataSchema.CollectionSchema.ShotRow[] sr = (DataSchema.CollectionSchema.ShotRow[])this.Coll.Shot.Select(sql);
            this.MyFactory.UpdateInternalList(sr, fi, link);
            return sr;
        }

        #endregion

   }
}
